"use strict";
const passwordHash = require('yi-password-hash');
const async = require('async');

const ObjectID = require('mongodb').ObjectID;
const miniprogram= require('yi-miniprogram');
const userAuth = require('yi-user-auth');

module.exports = {
    //open:true,
    //security: true,
    description: '微信登录',
    handler(req, res, next) {
        const code = req.body.code;

        miniprogram(code, req.config.miniprogram, (e, r) => {
            if (e) {
                return res.reply({code: 1002, message: e, result:null});
            }

            let openId = r.openid;
            let session_key = r.session_key;
            let unionid = r.unionid;

            console.log("获取到openid:::::::::::::", openId);
            req.mongo.collection('user').find({openid: openId}).limit(1).next((err, user) => {
                    if(!user){
                        let id= new ObjectID();
                        user={
                            _id:id,
                            openid:openId,
                            session_key:session_key,
                            unionid:unionid,
                            role:'user',
                            createTime: new Date(),
                            tel:""
                        };
                        req.mongo.collection('user').insertOne(user,function (e,r) {
                            console.log(r);
                            userAuth(user,req,(e,r)=>{
                                if(e)
                                    res.err.unknownError(e);
                                else{
                                    delete user.pwd;
                                    res.reply({code:0,message:"登录成功",data:r});
                                }
                            });

                        });
                    }else{
                        user.role='user';
                        console.log("根据openid查询到用户信息:::::::::::::", user);
                        if(user.session_key!== session_key){
                            user.session_key = session_key;
                            req.mongo.collection('user').updateOne({openid: openId},{$set:{session_key:session_key}},function (e0,r0) {

                                console.log("更新session_key:::::::::::::", session_key);
                                userAuth(user,req,(e,r)=>{
                                    console.log("userAuth鉴权成功:::::::::::::", session_key);
                                    if(e)
                                        res.err.unknownError(e);
                                    else{
                                        delete user.pwd;
                                        res.reply({code:0,message:"登录成功",data:r});
                                    }
                                });

                            });
                        }else{
                            userAuth(user,req,(e,r)=>{
                                console.log("userAuth鉴权成功:::::::::::::", session_key);
                                if(e)
                                    res.err.unknownError(e);
                                else{
                                    delete user.pwd;
                                    res.reply({code:0,message:"登录成功",data:r});
                                }
                            });
                        }
                    }
                });



        })
    }
};
